﻿using FRMS.DAL;
using FRMS.Model;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.BUS
{
    public class RequsetBUS
    {
        public string Base64ToImage(string dataURL, string webRootPath)
        {
            try
            {
                dataURL = dataURL.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "");//将base64头部信息替换
                byte[] bytes = Convert.FromBase64String(dataURL);
                MemoryStream memStream = new MemoryStream(bytes);
                Image mImage = Image.FromStream(memStream);

                Bitmap bp = new Bitmap(mImage);

                String Tpath = "/img/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
                string FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                string FilePath = webRootPath + "/wwwroot" + Tpath;
                DirectoryInfo di = new DirectoryInfo(FilePath);


                if (!di.Exists) { di.Create(); }

                using (FileStream fs = File.Create(FilePath + FileName + ".jpg"))
                {
                    // 复制文件
                    bp.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
                    // 清空缓冲区数据
                    bp.Dispose();
                    fs.Flush();
                    fs.Close();
                }

                return Tpath + FileName + ".jpg";//返回相对路径 

            }
            catch (Exception e)
            {
                return "";
            }
        }

        //public AddAuditInfoReturn addAuditInfo(AddAuditInfo addAuditInfo)
        //{
        //    AddAuditInfoReturn addAuditInfoReturn = new AddAuditInfoReturn();
        //    addAuditInfoReturn.method = "addaudit";
        //    RecordDAL recordDAL = new RecordDAL();
        //    int projectid = recordDAL.QueryProjectID(addAuditInfo.ProjectKey);
        //    if (projectid > 0)
        //    {
        //        int auditid = recordDAL.QueryAuditID(addAuditInfo.data.AuditID);
        //        if (auditid == 0)
        //        {
        //            bool result = recordDAL.AddAuditInfo(addAuditInfo.data, projectid);
        //            if (result)
        //            {
        //                QRCodeAuditInfoReturnData data = new QRCodeAuditInfoReturnData();
        //                addAuditInfoReturn.code = 0;
        //                data.AuditID = addAuditInfo.data.AuditID;
        //                data.ProjectKey = addAuditInfo.ProjectKey;
        //                addAuditInfoReturn.data = data;
        //            }
        //            else
        //            {
        //                addAuditInfoReturn.code = 1;
        //                addAuditInfoReturn.msg = "同步审核记录失败";
        //            }
        //        }
        //        else
        //        {
        //            bool result = recordDAL.UpdateAuditInfo(addAuditInfo.data);
        //            if (result)
        //            {
        //                addAuditInfoReturn.code = 0;
        //            }
        //            else
        //            {
        //                addAuditInfoReturn.code = 1;
        //                addAuditInfoReturn.msg = "同步更新审核记录失败";
        //            }
        //        }
        //    }
        //    else
        //    {
        //        addAuditInfoReturn.code = 1;
        //        addAuditInfoReturn.msg = "不存在该项目";
        //    }
        //    return addAuditInfoReturn;
        //}

        
    }
}
